﻿namespace ServiceCore.WebApi.Controllers
{
    public static class UtilityFunction
    {
        private static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();

        #region 锁扩展

        /// <summary>
        /// 锁扩展
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="IsShowException"></param>
        public static void LockExtension(
                                        System.Action callback, bool IsShowException = false
                                        )
        {
            try
            {
                LogWriteLock.EnterWriteLock();
                callback();
            }
            catch
            {
                if (IsShowException)
                    throw;
            }
            finally
            {
                LogWriteLock.ExitWriteLock();
            }
        }

        #endregion 锁扩展

        #region 循环遍历扩展

        /// <summary>
        /// 循环遍历扩展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lst"></param>
        /// <param name="callback"></param>
        public static void ForeachExtension<T>
        (
            this List<T> lst,
            Action callback
        )
        {
            try
            {
                callback();
            }
            catch (Exception ex)
            {
                //ex.ToJson().LogWriteService(LogServiceCore.LogServiceCore_Exception);
            }
        }

        #endregion 循环遍历扩展

        #region 定时循环

        /// <summary>
        /// 定时循环
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="PollCount">循环多少次</param>
        /// <param name="IntevalSecond">每隔几秒循环一次</param>
        public static void Loop_Poll(
           System.Func<int, Boolean> callback,
           int PollCount,
           int IntevalSecond = 5000
           )
        {
            System.Threading.Thread.Sleep(IntevalSecond);
            var count = 0;
        a:;
            if (callback(count) == false)
            {
                if (count < PollCount)
                {
                    count = count + 1;
                    System.Threading.Thread.Sleep(IntevalSecond);
                    goto a;
                }
            }
        }

        #endregion 定时循环


        private static ReaderWriterLockSlim LogRequestLog = new ReaderWriterLockSlim();

        #region 请求锁扩展

        /// <summary>
        /// 请求锁扩展
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="IsShowException"></param>
        public static void LockExtensionRequest(
                                        System.Action callback, bool IsShowException = false
                                        )
        {
            try
            {
                LogRequestLog.EnterWriteLock();
                callback();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                LogRequestLog.ExitWriteLock();
            }
        }

        #endregion 锁扩展
    }
}
